package com.liangzhong.model.compete
{
	import com.liangzhong.constants.ClubConst;
	import com.liangzhong.constants.ResourceConst;
	import com.liangzhong.manager.ResourceManager;
	import com.liangzhong.model.AppData;
	import com.liangzhong.model.buffer.vo.BufferVo;
	import com.liangzhong.model.compete.vo.ChallengeCostVo;
	import com.liangzhong.model.compete.vo.CompeteInfoVo;
	import com.liangzhong.model.compete.vo.InlandRankVo;
	import com.liangzhong.model.compete.vo.NationRankvo;
	import com.liangzhong.model.compete.vo.NationTaskVo;
	import com.liangzhong.model.ladderMatch.vo.MatchTimeVo;
	
	import flash.utils.ByteArray;
	import flash.utils.Dictionary;

	public class CompeteDataManager
	{
		
		
		
		
		
		
		//中国点
		public static const CHINEPOINT:Array = [[908,789],
			[976,616],
			[1013,718],
			[1026,522],
			[1097,489],
			[883,593],
			[802,692],
			[591,536],
			[669,659],
			[1016,800]];
		/**
		 *英国 
		 */		
		public static const ENGLANDPOINT:Array = [[854,664],
			[853,561],
			[814,751],
			[931,738],
			[886,820],
			[764,534],
			[766,815],
			[826,457],
			[741,655],
			[657,537]];
		/**
		 * 意大利 
		 */		
		public static const ITALYpoint:Array = [[909,612],
			[802,602],
			[1009,673],
			[999,787],
			[930,668],
			[1097,715],
			[832,463],
			[705,454],
			[840,836],
			[924,869]
		];
		/**
		 *法国  
		 */		
		public static const FRANCEPOINT:Array = [[829,609],
			[829,512],
			[764,755],
			[903,692],
			[976,537],
			[590,524],
			[965,771],
			[784,810],
			[678,601],
			[751,448]];
		/**
		 *西班牙 
		 */		
		public static const SPAINPOINT:Array = [[817,584],
			[816,489],
			[708,642],
			[882,650],
			[779,703],
			[945,517],
			[749,822],
			[883,782],
			[1063,481],
			[959,709]];
		
		/**
		 *德国 
		 */		
		public static const GERMANYPOINT:Array = [[831,612],
			[835,489],
			[717,670],
			[953,639],
			[764,773],
			[818,843],
			[829,698],
			[948,803],
			[945,512],
			[720,536]];
		
		private var _competeInfoVo:CompeteInfoVo;
		/**
		 *对手排名列表 
		 */
		public var opponentRankList:Array;
		/**
		 *国家间的排名 
		 */
		public var nationRankList:Array;
		
		
		/**
		 *国家挑战赛消耗产出 
		 */
		private var _challengeDic:Dictionary;
		/**
		 *任务 
		 */
		private var _taskDic:Dictionary;
		private var _selectChallengCost:ChallengeCostVo;
		
		/**
		 *更多的排名时间 
		 */
		public var moreinlandRankTime:int;
		/**
		 *当前排名
		 * 半小时更新一次  
		 */
		public var currList:Array;
		/**
		 *上次排名  
		 */
		public var previousList:Array;
		
		/**
		 *比赛时间 
		 */
		public var matchTimeList:Array;
		
		
		/**
		 *国家间的排名 
		 */
		public var nationRank:int;
		public function CompeteDataManager()
		{
		}
		
		/**
		 *国家对抗赛信息 
		 * 
		 */
		public function analysiscompeteInfo($obj:Object):void
		{
			if(_competeInfoVo == null)
			{
				_competeInfoVo = new CompeteInfoVo();
			}
			_competeInfoVo.rivalcountry = $obj.rivalcountry;
			_competeInfoVo.rivalnationprerank = $obj.rivalnationprerank;
			_competeInfoVo.homecoin = $obj.homecoin;
			_competeInfoVo.analysis($obj.self);
			if(opponentRankList == null)opponentRankList = [];
			opponentRankList.splice(0,opponentRankList.length);
			var inlandVo:InlandRankVo;
			var arr:Array = $obj.rivalclub;
			var len:int;
			if(arr != null)
			{
				len = arr.length;
				var i:int;
				for(i = 0;i < len;i++)
				{
					inlandVo = new InlandRankVo();
					inlandVo.analysis(arr[i]);
					opponentRankList.push(inlandVo);
				}
			}
			
			if(nationRankList == null)nationRankList  = [];
			nationRankList.splice(0,nationRankList.length);
			arr = $obj.nationrank;
			len = arr.length;
			var nationVo:NationRankvo;
			for(i = 0;i < len;i++)
			{
				nationVo = new NationRankvo();
				nationVo.analysis(arr[i]);
				nationRankList.push(nationVo);
				if(AppData.getInstance().clubDatamanager.clubInfo.country == nationVo.country)
				{
					nationRank = nationVo.nationrank;
				}
				
			}
			opponentRankList.sortOn("sort",Array.NUMERIC);
			nationRankList.sortOn("nationrank",Array.NUMERIC);
			arr = $obj.self.task;
			len = arr.length;
			for(i = 0;i < len;i++)
			{
				issueNationTask(arr[i]);
			}
			analysisLadderTime();
		}
		/**
		 * 根据id获取对手的排名 
		 * @param $id
		 * @return 
		 * 
		 */		
		public function getopponentRankSort($id:int):int
		{
			for each(var inlandVo:InlandRankVo in opponentRankList)
			{
				if(inlandVo.clubid == $id)
				{
					
					return inlandVo.sort;
				}
			}
			return 1;
		}
		/**
		 *解析国家排名 
		 * 
		 */
		public function analysisnationRank($arr:Array):Array
		{
			var len:int = $arr.length;
			var vo:InlandRankVo;
			var arr:Array = [];
			for(var i:int = 0;i < len;i++)
			{
				vo = new InlandRankVo();
				vo.analysis($arr[i]);
				arr.push(vo);
			}
			arr.sortOn("sort",Array.NUMERIC);
			return arr;
		}
		
		/**
		 *获取国家描述 
		 * 
		 */
		public static function getCountryDes($id:int):String
		{
			switch($id)
			{
				case ClubConst.COUNTRY_ENGLAND:
					return "英超总是被认为是世界上最好的联赛，其特点为激烈的身体对抗和" +
						"快速的攻防转换。现时英超联赛已经成为世界上最受欢迎的体育赛事，" +
						"同电视台的亲密合作也促成英超成为世界收入最高的足球联赛！代表俱乐" +
						"部曼联，切尔西，阿森纳等。";
					break;
				case ClubConst.COUNTRY_SPAIN:
					return "西甲是目前欧洲所有联赛中中最具欧战竞争力的联赛，共获得过13次欧冠奖杯，" +
						"其特点为技术细腻，配合行云流水。素有“明星联赛”，“先生联赛”" +
						"之称，是培养足球先生和金球奖的摇篮！代表俱乐部巴萨，皇马，瓦伦西亚等。";
					break;
				case ClubConst.COUNTRY_FRANCE:
					return "法甲一直以来没有产生一支长期雄霸联赛的超级强队，" +
						"浪漫的天性与懒散的作风是其特点，但有时也会给人们带来意想不到的结局。" +
						"欧洲五大联赛中最需要证明自己的联赛！代表俱乐部巴黎圣日耳曼，马赛，里昂等。";
					break;
				case ClubConst.COUNTRY_GERMANY:
					return "德国是从大不列颠岛上引进足球的第一个欧洲内陆国家，然而也是欧洲五" +
						"大联赛中历史最年轻的。其特点为富有韧性，" +
						"坚毅，有血性，经常能上演逆转！近期摘取欧冠一度让德甲风光无二！" +
						"代表俱乐部拜仁，多特蒙德，勒沃库森等。";
					break;
				case ClubConst.COUNTRY_ITALY:
					return "意甲一向是世界上水平最高的职业足球联赛之一，其特点为注重防守。" +
						"上世纪八、九十年代，意甲球星云集，实力强劲的球队众多，一度被公认为是世界第一足" +
						"球联赛，被誉为“小世界杯”。万能的国王，您能挑战意甲的权威，带领您的联赛走向世" +
						"界的巅峰么？";
					break;
				case ClubConst.COUNTRY_CHAIN:
					return "中超脱胎自中国足球甲级A组联赛，在经历了一些列成长的烦恼之" +
						"后中超以新的姿态屹立在亚洲足坛！坐拥世界最大规模的球迷团体，欲火重生" +
						"之后的中超会带给越来越多的惊喜！代表俱乐部广州恒大，上海申花，山东鲁能等。";
					break;
			}
			return "：";
		}
		/**
		 * 根据国家对抗赛排名获取buffer说明  
		 * @param $rank
		 * @return 
		 * 
		 */		
		public static function getcompeteBuffer($rank:int):String
		{
			var buffVo:BufferVo;
			var str:String = "";
			switch($rank)
			{
				case 1:
					str = "国家挑战冠军\n\n";
					buffVo = AppData.getInstance().bufferManager.getBufferById(111);
					break;
				case 2:
					str = "国家挑战亚军\n\n";
					buffVo = AppData.getInstance().bufferManager.getBufferById(112);
					break;
				case 3:
					str = "国家挑战季军\n\n";
					buffVo = AppData.getInstance().bufferManager.getBufferById(113);
					break;
				case 4:
					str = "国家挑战第四名\n\n";
					buffVo = AppData.getInstance().bufferManager.getBufferById(114);
					break;
				case 5:
					str = "国家挑战第五名\n\n";
					buffVo = AppData.getInstance().bufferManager.getBufferById(115);
					break;
				case 6:
					str = "国家挑战第六名\n\n";
					buffVo = AppData.getInstance().bufferManager.getBufferById(116);
					break;
			}
			
			if(buffVo != null)
			{
				return "<p align='left'>" + str +buffVo.bufferTips +"</p>";
			}
			return "";
		}

		
		
		/**
		 * 根据国家对抗赛排名获取buffer说明  
		 * @param $rank
		 * @return 
		 * 
		 */		
		public static function getbufferTipsIsCountry($rank:int):String
		{
			var buffVo:BufferVo;
			var str:String = "";
			switch($rank)
			{
				case 1:
					str = "国家挑战冠军\n";
					buffVo = AppData.getInstance().bufferManager.getBufferById(111);
					break;
				case 2:
					str = "国家挑战亚军\n";
					buffVo = AppData.getInstance().bufferManager.getBufferById(112);
					break;
				case 3:
					str = "国家挑战季军\n";
					buffVo = AppData.getInstance().bufferManager.getBufferById(113);
					break;
				case 4:
					str = "国家挑战第四名\n";
					buffVo = AppData.getInstance().bufferManager.getBufferById(114);
					break;
				case 5:
					str = "国家挑战第五名\n";
					buffVo = AppData.getInstance().bufferManager.getBufferById(115);
					break;
				case 6:
					str = "国家挑战第六名\n";
					buffVo = AppData.getInstance().bufferManager.getBufferById(116);
					break;
			}
			
			if(buffVo != null)
			{
				return "<p align='left'>" + str +buffVo.bufferTipsIsCountry +"</p>";
			}
			return "";
		}

		/**
		 *国家对抗赛信息 
		 */
		public function get competeInfoVo():CompeteInfoVo
		{
			return _competeInfoVo;
		}

		
		/**
		 * 挑战消耗 
		 * 
		 */		
		private function challengeDic():void
		{
			_challengeDic = new Dictionary();
			var xml:XML = ResourceManager.instance.getResource(ResourceConst.CHALLENGE_XML);
			var chxmL:XMLList = xml.challenge;
			var vo:ChallengeCostVo;
			for each(var item:XML in chxmL["item"])
			{
				vo = new ChallengeCostVo();
				vo.analysi(item);
				_challengeDic[vo.sort] = vo;
			}
		}
		/**
		 *获取挑战消耗 
		 * @param $sort
		 * 
		 */
		public function getChallengeCost($sort:int):ChallengeCostVo
		{
			if(_challengeDic == null)challengeDic();
			return _challengeDic[$sort];
			
			
		}
		/**
		 *选择了挑战消耗 
		 * @param $sort
		 * 
		 */
		public function selectchallengecost($sort:int):void
		{
			_selectChallengCost= getChallengeCost($sort);
		}

		/**
		 *选择的挑战消耗 
		 */
		public function get selectChallengCost():ChallengeCostVo
		{
			if(_selectChallengCost == null)selectchallengecost(20);
			return _selectChallengCost;
		}
		/**
		 *任务 
		 * 
		 */		
		private function taskDic():void
		{
			_taskDic = new Dictionary();
			var xml:XML = ResourceManager.instance.getResource(ResourceConst.CHALLENGE_XML);
			var chxmL:XMLList = xml.task;
			var vo:NationTaskVo;
			for each(var item:XML in chxmL["item"])
			{
				vo = new NationTaskVo();
				vo.analysi(item);
				_taskDic[vo.id] = vo;
			}
		}
		
		
		/**
		 *发布国家任务 
		 * 
		 */
		public function issueNationTask($id:int):void
		{
			var nationtaskVo:NationTaskVo = getTaskByid($id);
			if(nationtaskVo == null)return;
			nationtaskVo.taskStatus = false;
		}
		/**
		 *获取国家任务VO 
		 * @param $id
		 * @return 
		 * 
		 */
		public function getTaskByid($id:int):NationTaskVo
		{
			if(_taskDic == null)taskDic();
			return _taskDic[$id];
		}
		/**
		 *解析当前排名 
		 * @param $arr
		 * 
		 */
		public function analyscurrentList($arr:Array):void
		{
			if(currList == null)currList = [];
			currList.splice(0,currList.length);
			var len:int = $arr.length;
			var vo:InlandRankVo;
			for(var i:int = 0;i < len;i++)
			{
				vo = new InlandRankVo();
				vo.analysis($arr[i]);
				currList.push(vo);
			}
//			currList.sortOn("sort",Array.NUMERIC);
			currList.sort(sort);
		}
		
		
		/**
		 *重新当前的排名 (根据挑战点)
		 * 
		 */
		public function rerankCurrent():void
		{
			
			if(currList == null)return;
			var isRe:Boolean = false;
			for each(var vo:InlandRankVo in currList)
			{
				if(vo.clubid == AppData.getInstance().clubDatamanager.clubInfo.clubid)
				{
					vo.rival += competeInfoVo.rival;
					isRe = true;
					break;
				}
			}
			if(!isRe)return;
			currList.sort(sortByRival);
			
		}
		
		private function sortByRival(a:InlandRankVo,b:InlandRankVo):Number
		{
			if(a.rival > b.rival)return -1;
			if(b.rival < b.rival)return 1;
			return 0;
		}
		/**
		 *上界的排名  
		 * @param $arr
		 * 
		 */
		public function analysipreviousList($arr:Array):void
		{
			if(previousList== null)previousList = [];
			previousList.splice(0,previousList.length);
			var len:int = $arr.length;
			var vo:InlandRankVo;
			for(var i:int = 0;i < len;i++)
			{
				vo = new InlandRankVo();
				vo.analysis($arr[i]);
				previousList.push(vo);
			}
			//previousList.sortOn("sort",Array.NUMERIC);
			
			previousList.sort(sort);
		}
		private function sort(a:InlandRankVo,b:InlandRankVo):Number
		{
			if(a.sort > b.sort)return 1;
			if(a.sort < b.sort)return -1;
			return 0;
		}
		

		
		/**
		 *解析比赛时间 
		 * 
		 */
		public function analysisLadderTime():void
		{
			if(matchTimeList != null)return;
			matchTimeList = [];
			var xml:XML = ResourceManager.instance.getResource(ResourceConst.LADDERMATCHTIME);
			//			trace(xml.seasonTime);
			var timeXml:XML = XML(ResourceManager.instance.getResource(ResourceConst.LADDERMATCHTIME)).competeTime[0];
			var releaseVersion :String;
			var vo:MatchTimeVo;
			for each(var item :XML in timeXml.item)
			{
				vo = new MatchTimeVo();
				vo.starTime = item.@starTime;
				vo.endTime = item.@endTime;
				matchTimeList.push(vo);
			}
		}
	}
}